#!/usr/bin/env python3
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.


import requests

# Define the URL prefix for API requests
url_prefix = 'http://'

class DataApiClient:
    def __init__(self, host, port):
        """Initialize Data API client"""
        self.host = host
        self.port = port

    def es_search(self, index: str, request_json) -> dict:
        """Perform a search on the specified Elasticsearch index.
        :param index: The name of the Elasticsearch index to search.
        :param request_json: The JSON request body for the search.
        :return: The response from the Elasticsearch search as a dictionary.
        """
        url = f"{url_prefix}{self.host}:{self.port}/data_api/es/{index}/_search"
        response = requests.post(url, json=request_json)
        return response.text

    def sql_search(self, request_json):
        """Execute a a SQL search query against the API.

        :param request_json: The JSON request body for the SQL query.
        :return: The response from the SQL search as text.
        """
        url = f"{url_prefix}{self.host}:{self.port}/data_api/_opendistro/_sql"
        response = requests.post(url, json=request_json)
        return response.text

